/*
 * Generated via the com.flca generator
 */
package flca.voicenav.srv;
 
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.codehaus.jackson.map.ObjectMapper;

import com.flca.frw.log.ILog;
import com.flca.frw.log.LogHelper;

import flca.osm.perst.model.JsonArrayPNode;
import flca.voicenav.VoiceNavContext;
import flca.voicenav.model.TrkPt;
/**
 * 
 *
 * @author nly36776
 * @version $Id: RestService.jet,v 1.1 rbakkerus Exp $
 * @generated
 */ 
@Path("/rest")
public class RestVoiceNavServices 
{
	private static ILog logger = LogHelper.getLogger(RestVoiceNavServices.class);
	private static ObjectMapper om = new ObjectMapper();
	private static VoiceNavServices sService;
	
	protected static VoiceNavServices getService()
	{
		if (sService == null) {
			sService = VoiceNavContext.INSTANCE.getInjector().getInstance(VoiceNavServices.class);
		}
		
		return sService;
	}
	   			
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("ping")
	public String ping(@QueryParam("aMsg") String aMsg) 
	{
		logger.info("starting Rest service ping");
		try {
			String _aMsg = om.readValue(aMsg, String.class);
			String result = getService().ping(_aMsg);
			return om.writeValueAsString(result);
		} catch(Exception ex) {
			logger.error("error in Rest service ping ",ex);
			throw new RuntimeException(ex);
		}
	}   	
	
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	@Path("optimizeRoute")
	public String optimizeRoute(@QueryParam("aRoute") JsonArrayPNode aRoute) 
	{
		logger.info("starting Rest service optimizeRoute");
		try {
			System.out.println("starting optimizeRoute");
			System.out.println("read route " + aRoute.size());
			List<TrkPt> optimizedRoute = getService().optimizeRoute(aRoute);
			System.out.println("route optimized" + optimizedRoute.size() + "  " + optimizedRoute);
			String result = om.writeValueAsString(optimizedRoute);
			return result;
		} catch(Exception ex) {
			logger.error("error in Rest service optimizeRoute ",ex);
			throw new RuntimeException(ex);
		}
	}
	
	public boolean startupTest() {
		if (getService() != null) {
			return true;
		} else {
			return false;
		}
	}
}
